'use strict';

const XLayout = require('@tuniu/xlayout');
const co = require('co');

module.exports = function(config, lightMvc) {
    let core = lightMvc.getAppContext('core');
    const xlayout = XLayout(config.cdn);
    xlayout.setDefaultLayout('pc');
    xlayout.register('pc', {
        blocks: {
            header: {
                runAfterData: function(viewData) {
                    viewData = viewData || {};
                    let _ = this;
                    let cityCode = new Buffer(unescape(this.req.cookies[config.cookies['cityCode'].name] || ''), "base64").toString();
                    return co(function*() {
                        if (!cityCode) {
                            try {
                                let remoteSrv = ctx.getService('tuniu.services.RemoteService');
                                let location = yield core.getServiceContext(remoteSrv).getLocationP(_.req);
                                cityCode = location.cityCode;
                            } catch (err) {
                                cityCode = 2500;
                            }
                        }

                        let commonSrv = _.res.locals.ctx.getService('tuniu.services.CommonService');
                        let header = yield commonSrv.getHeaderP(cityCode, viewData.from);

                        let UserAuth = lightMvc.getAppContext('UserAuth');
                        let auth = UserAuth.create(_.req, _.res, _.next);
                        header.user = yield auth.getLoginUserP();
                        header.isLogin = !!header.user;
                        cityCode = header.cityCode ? new Buffer(String(header.cityCode)).toString('base64') : '';
                        _.res.cookie(config.cookies['cityCode'].name, cityCode, config.cookies['cityCode'].setting);
                        return {
                            headerData: header
                        };
                    });
                }
            },
            footer: {
                runAfterData: function(viewData) {
                    let _ = this;
                    viewData = viewData || {};
                    let cityCode = new Buffer(unescape(this.req.cookies[config.cookies['cityCode'].name] || ''), "base64").toString();
                    return co(function*() {
                        if (!cityCode) {
                            try {
                                let remoteSrv = ctx.getService('tuniu.services.RemoteService');
                                let location = yield core.getServiceContext(remoteSrv).getLocationP(_.req);
                                cityCode = location.cityCode;
                            } catch (err) {
                                cityCode = 2500;
                            }
                        }
                        let commonSrv = _.res.locals.ctx.getService('tuniu.services.CommonService');
                        let footer = yield commonSrv.getFooterP(cityCode, viewData.from);
                        return {
                            footerData: footer
                        };
                    });
                }
            }
        }
    });
    xlayout.register('another', {
        path: 'kickstart/layout/another/layout.ejs',
        blocks: {
            header: {
                path: 'kickstart/layout/another/header.ejs'
            },
            footer: {
                path: 'kickstart/layout/another/footer.ejs'
            }
        }
    });
    return xlayout.pipe;
}